package com.tax.common.util;

import org.apache.commons.lang3.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class DateUtil {

    public static final String PATTERN_yyyyMM = "yyyyMM";
    public static final String PATTERN_yyyy_MM = "yyyy-MM";
    public static final String PATTERN_yyyy_MM_dd = "yyyy-MM-dd";
    public static final String PATTERN_yyyyMMddHHmmss = "yyyyMMddHHmmss";
    public static final String PATTERN_yyyy_MM_dd_HHmmss = "yyyy-MM-dd HH:mm:ss";
    public static final String PATTERN_yyyy_MM_dd_HHmm = "yyyy-MM-dd HH:mm";
    public static final String PATTERN_yyyy_MM_ddHHmmss = "yyyy-MM-ddHH:mm:ss";
    public static final String PATTERN_yyyyMMdd = "yyyyMMdd";
    public static final String PATTERN_yyyyMMddHHmmssSSS = "yyyyMMddHHmmssSSS";
    public static final String PATTERN_HTTP = "EEE, dd MMM yyyy HH:mm:ss zzz";
    public static final String PATTERN_HHmm = "HH:mm";

    public static String getCurrentTimeString() {
        return parseDateToString(Calendar.getInstance().getTime(), PATTERN_yyyyMMddHHmmssSSS);
    }

    public static String dateMinutesModify(Date source, Integer minutes, String pattern) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(source);
        calendar.add(Calendar.MINUTE, minutes);
        return parseDateToString(calendar.getTime(), pattern);
    }

    public static String dateMinutesModify(String source, Integer minutes, String pattern) {
        Date date = null;
        try {
            date = parseStringToDate(source, pattern);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MINUTE, minutes);
        return parseDateToString(calendar.getTime(), pattern);
    }

    public static String parseDateToString(Date date, String pattern) {
        if (date == null) {
            return "";
        }
        SimpleDateFormat _df = new SimpleDateFormat(pattern);
        return _df.format(date);
    }

    public static String parseDateToStringDateTime(Date date) {
        if (date == null) {
            return "";
        }
        SimpleDateFormat _df = new SimpleDateFormat(PATTERN_yyyy_MM_dd_HHmmss);
        return _df.format(date);
    }

    /**
     * 获取某一天结束时间
     * @param date
     * @return
     */
    public static Date getDayEnd(Date date){
        if(date==null){
            date=new Date();
        }
        Calendar currentDate = new GregorianCalendar();
        currentDate.setTime(date);
        currentDate.set(Calendar.HOUR_OF_DAY, 23);
        currentDate.set(Calendar.MINUTE, 59);
        currentDate.set(Calendar.SECOND, 59);
        return currentDate.getTime();
    }

    /**
     * 获取某一天开始时间
     * @param date
     * @return
     */
    public static Date getDayStart(Date date) {
        if (date == null) {
            date = new Date();
        }

        Calendar currentDate = new GregorianCalendar();
        currentDate.setTime(date);
        currentDate.set(11, 0);
        currentDate.set(12, 0);
        currentDate.set(13, 0);
        return currentDate.getTime();
    }

    /**
     * 当前剩余时间：秒
     * @return
     */
    public static Long getTodayRemainSeconds(){
        Date currDate = Calendar.getInstance().getTime();
        Date todayEndDate = getDayEnd(currDate);
        Long result = todayEndDate.getTime() - currDate.getTime();
        return result/1000;
    }

    public final static long getDateLong() {
        return (new Date()).getTime();
    }

    public static Date format(Date date, String pattern) {
        if (date == null) {
            return date;
        }
        SimpleDateFormat _df = new SimpleDateFormat(pattern);
        _df.format(date);
        return date;
    }

    public static double getCurrentTime_fortoken() {
        return Double.parseDouble(getCurrentTimeString().substring(0, 13));
    }

    public static String parseDateToHttp(Date date) {
        if (date == null) {
            return "";
        }
        SimpleDateFormat _df = new SimpleDateFormat(PATTERN_HTTP, Locale.US);
        return _df.format(date);
    }

    public static Date parseLongToDate(long time) {
        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(time);
        return cal.getTime();
    }

    /**
     * 使用参数Format将字符串转为Date
     * 
     * @throws ParseException
     */
    public static Date parseStringToDate(String strDate, String pattern) throws ParseException {
        SimpleDateFormat _df = new SimpleDateFormat(pattern);
        return _df.parse(strDate);
    }

    public static long getTimeDiff(Date begin_date, Date end_date) {
        return end_date.getTime() - begin_date.getTime();
    }

    /**
     * 根据开始时间和结束时间返回时间段内的时间集合
     *
     * @param beginDate
     * @param endDate
     * @return List
     */
    public static List<Date> getDatesBetweenTwoDate(Date beginDate, Date endDate) {
        List<Date> lDate = new ArrayList<Date>();
        lDate.add(beginDate);// 把开始时间加入集合
        Calendar cal = Calendar.getInstance();
        // 使用给定的 Date 设置此 Calendar 的时间
        cal.setTime(beginDate);
        boolean bContinue = true;
        while (bContinue) {
            // 根据日历的规则，为给定的日历字段添加或减去指定的时间量
            cal.add(Calendar.DAY_OF_MONTH, 1);
            // 测试此日期是否在指定日期之后
            if (endDate.after(cal.getTime())) {
                lDate.add(cal.getTime());
            } else {
                break;
            }
        }
        lDate.add(endDate);// 把结束时间加入集合
        return lDate;
    }

    /**
     * 获取两个时间之间的天数
     * @param beginDate
     * @param endDate
     * @return
     */
    public static Integer getTwoDaysInterval(Date beginDate, Date endDate){
        Calendar cal = Calendar.getInstance();
        cal.setTime(endDate);
        long time1 = cal.getTimeInMillis();
        cal.setTime(beginDate);
        long time2 = cal.getTimeInMillis();
        long between_days=(time1-time2)/(1000*3600*24);
        int parseInt = Integer.parseInt(String.valueOf(between_days));
//        parseInt = parseInt + 1;
        return parseInt ;
    }
    /**
     * 根据开始时间和结束时间返回时间段内的时间集合
     *
     * @param beginDate
     * @param endDate
     * @return List
     */
    public static List<String> getDatesBetweenTwoDate(String beginDate, String endDate) {
        List<String> lDate = new ArrayList<String>();
        try {
            if (beginDate.compareTo(endDate) > 0) {
                return lDate;
            }

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

            lDate.add(beginDate);// 把开始时间加入集合
            Calendar cal = Calendar.getInstance();
            // 使用给定的 Date 设置此 Calendar 的时间
            cal.setTime(sdf.parse(beginDate));
            Date endDate0 = sdf.parse(endDate);
            boolean bContinue = true;
            while (bContinue) {
                // 根据日历的规则，为给定的日历字段添加或减去指定的时间量
                cal.add(Calendar.DAY_OF_MONTH, 1);
                // 测试此日期是否在指定日期之后
                if (endDate0.after(cal.getTime())) {
                    lDate.add(sdf.format(cal.getTime()));
                } else {
                    break;
                }
            }
            if (!endDate.equals(beginDate)) {
                lDate.add(endDate);// 把结束时间加入集合
            }
        } catch (Exception e) {

        }
        return lDate;
    }

    /**
     * 获取两个日期相差的天数
     * 
     * @return
     */
    public static Integer getDifferentDays(Date beginDate, Date endDate) {
        int days = (int) ((endDate.getTime() - beginDate.getTime()) / (1000 * 3600 * 24));
        return days;
    }

    /**
     * 获取日期是周几
     * @param time
     * @return
     */
    public static String getWeekOfDate(Date time) {
        String[] weekDays = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
        Calendar cal = Calendar.getInstance();
        cal.setTime(time);
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0) {
            w = 0;
        }
        return weekDays[w];
    }
    
    /**
     * 根据传入的日期获取这个月所有的日期集合,转换成yyyy-MM-dd的形式
     * 
     * @param date
     * @return
     * @throws ParseException
     */
    public static List<String> getMonthDays(String date) throws ParseException{
    	List<String> list = new ArrayList<String>();
    	if (StringUtils.isEmpty(date)) {
			return list;
		}
		Date dates=new SimpleDateFormat("yyyy-MM-dd").parse(date);
		Calendar aCalendar = Calendar.getInstance(Locale.CHINA);
		aCalendar.setTime(dates);
		int year = aCalendar.get(Calendar.YEAR);//年份
		int month = aCalendar.get(Calendar.MONTH) + 1;//月份
		int day = aCalendar.getActualMaximum(Calendar.DATE);
		for (int i = 1; i <= day; i++) {
			if (month<10) {
				if (i<10) {
					String aDate = String.valueOf(year)+"-0"+month+"-0"+i;
					list.add(aDate);
					continue;
				}
				String aDate = String.valueOf(year)+"-0"+month+"-"+i;
				list.add(aDate);
			}else {
				if (i<10) {
					String aDate = String.valueOf(year)+"-"+month+"-0"+i;
					list.add(aDate);
					continue;
				}
				String aDate = String.valueOf(year)+"-"+month+"-"+i;
			    list.add(aDate);
			}
		}
		return list;
    }
    
    public static void main(String[] args) throws ParseException {
    	List<String> list=getMonthDays("2019-12-01");
    	list.forEach(a->{
    		System.out.println(a);
    	});
	}
}
